package com.example.zq.player.activity;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;

import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zq.player.R;
import com.example.zq.player.base.BasePager;
import com.example.zq.player.linked_database_interface.GlobalInformation;
import com.example.zq.player.linked_database_interface.LoginActivity;
import com.example.zq.player.linked_database_interface.base.BaseFragment;
import com.example.zq.player.linked_database_interface.base.RequestInterface;
import com.example.zq.player.linked_database_interface.base.ServerResponse;
import com.example.zq.player.linked_database_interface.bean.MyPageInfo;
import com.example.zq.player.linked_database_interface.bean.Product;
import com.example.zq.player.linked_database_interface.bean.ProductDetail;
import com.example.zq.player.linked_database_interface.bean.User;
import com.example.zq.player.pager.TypePager;
import com.example.zq.player.pager.MyPager;
import com.example.zq.player.pager.CartPager;
import com.example.zq.player.pager.HomePager;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

    //测试部分成员变量：
    Button button;

    private int position;
    private ArrayList<BaseFragment>  basePagers;
    private RadioGroup radioGroup;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //从服务端获取商品详情信息测试
//        setContentView(R.layout.test);
//        button = findViewById(R.id.button);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                RequestInterface.GetProductDetailInterface getProductDetailInterface = GlobalInformation.getRetrofit().create(RequestInterface.GetProductDetailInterface.class);
//                Call<ServerResponse<ProductDetail>> call = getProductDetailInterface.getProductDetail(2 + "");
//
//                call.enqueue(new Callback<ServerResponse<ProductDetail>>() {
//
//                    @Override
//                    public void onResponse(Call<ServerResponse<ProductDetail>> call, Response<ServerResponse<ProductDetail>> response) {
//                        if (response.body() == null) {
//                            Toast.makeText(MainActivity.this, "网络异常，请检查网络配置", Toast.LENGTH_SHORT).show();
//                            return;
//                        }
//
//                        TextView textView = findViewById(R.id.textView);
//                        textView.setText(
//                                response.body().getStatus()+"\n\n"+
//                                response.body().getMsg()+"\n\n"+
//                                response.body().getData().toString()+"\n\n"+
//                                response.body().getData().getName()+"\n\n"
//                        );
//                    }
//
//                    @Override
//                    public void onFailure(Call<ServerResponse<ProductDetail>> call, Throwable t) {
//                        Toast.makeText(MainActivity.this, "获取产品列表失败！", Toast.LENGTH_SHORT).show();
//                        Log.d("mError", "GetProductsListFailure: ");
//                    }
//                });
//            }
//        });






        //“我的页面”测试
//        Intent intent = new Intent(this, MeActivity.class);
//        this.startActivity(intent);
//        this.finish();

        //从服务器中拉取商品列表信息测试（后台）
//        setContentView(R.layout.test);
//        button = findViewById(R.id.button);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                RequestInterface.GetProductsListInterface getProductsListInterface = GlobalInformation.getRetrofit().create(RequestInterface.GetProductsListInterface.class);
//                Call<ServerResponse<MyPageInfo<Product>>> call = getProductsListInterface.getProductsList("1", "10");
//
//                call.enqueue(new Callback<ServerResponse<MyPageInfo<Product>>>() {
//
//                    @Override
//                    public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {
//                        if (response.body() == null) {
//                            Toast.makeText(MainActivity.this, "网络异常，请检查网络配置", Toast.LENGTH_SHORT).show();
//                            return;
//                        }
//                        MyPageInfo myPageInfo = response.body().getData();
//
////                        Toast.makeText(MainActivity.this, response.body().getStatus()+"", Toast.LENGTH_SHORT).show();
//                        Toast.makeText(MainActivity.this, myPageInfo.toString()+"", Toast.LENGTH_SHORT).show();
//
//                        TextView textView = findViewById(R.id.textView);
//                        textView.setText(
//                                response.body().getStatus()+"\n\n"+
//                                response.body().getMsg()+"\n\n"+
//                                response.body().getData().toString()+"\n\n"+
//                                response.body().getData().getList().toString()+"\n\n"
//                        );
//                    }
//
//                    @Override
//                    public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
//                        Toast.makeText(MainActivity.this, "获取产品列表失败！", Toast.LENGTH_SHORT).show();
//                        Log.d("mError", "GetProductsListFailure: ");
//                    }
//                });
//            }
//        });

        //真正需要的代码：
        if(GlobalInformation.getUser() == null){
            //登录测试
            Intent intent = new Intent(this, LoginActivity.class);
            this.startActivity(intent);
            this.finish();
        }else {
            //钟强的代码
            setContentView(R.layout.activity_main);
            initView();
            initFragment();
            setListener();
        }
    }

    public void setListener() {
        radioGroup.setOnCheckedChangeListener(new myOnCheckedChangeListener());
        radioGroup.check(R.id.rb_home);
    }
    class myOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch(i){
                default:
                    position=0;
                    break;
                case R.id.rb_type:
                    position=1;
                    break;
                case R.id.rb_cart:
                    position=2;
                    break;
                case R.id.rb_my:
                    position=3;
                    break;
            }
            //根据位置获得实列
            BaseFragment basePager = getFragment();
            //交换Fragment
            switchFragment(basePager);
        }
    }

    private void switchFragment(BaseFragment basePager) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_main_content,basePager);
        ft.commit();
    }

    private BaseFragment getFragment() {
        BaseFragment bs= basePagers.get(position);
        return bs;
    }

    private void initFragment() {
        basePagers = new ArrayList<>();
        basePagers.add(new HomePager());
        basePagers.add(new TypePager());
        basePagers.add(new CartPager());
        basePagers.add(new MyPager());
    }

    private void initView() {

        radioGroup = (RadioGroup) findViewById(R.id.rg_bottom_tag);

    }
}
